home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Graphics / sKulpt / skulpt-src / D3d-Util.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-18  |  8.7 KB  |  223 lines

  1. //-----------------------------------------------------------------------------
  2. // File: D3DUtil.cpp
  3. //
  4. // Desc: Shortcut macros and functions for using DX objects
  5. //
  6. //
  7. // Copyright (c) 1997-1999 Microsoft Corporation. All rights reserved
  8. //-----------------------------------------------------------------------------
  9. #define D3D_OVERLOADS
  10. #define STRICT
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <tchar.h>
  14. #include "D3DUtil.h"
  15.  
  16. //-----------------------------------------------------------------------------
  17. // Name: D3DUtil_InitSurfaceDesc()
  18. // Desc: Helper function called to build a DDSURFACEDESC2 structure,
  19. //       typically before calling CreateSurface() or GetSurfaceDesc()
  20. //-----------------------------------------------------------------------------
  21. VOID D3DUtil_InitSurfaceDesc( DDSURFACEDESC2& ddsd, DWORD dwFlags,
  22.                               DWORD dwCaps )
  23. {
  24.     ZeroMemory( &ddsd, sizeof(ddsd) );
  25.     ddsd.dwSize                 = sizeof(ddsd);
  26.     ddsd.dwFlags                = dwFlags;
  27.     ddsd.ddsCaps.dwCaps         = dwCaps;
  28.     ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  29. }
  30.  
  31. //-----------------------------------------------------------------------------
  32. // Name: D3DUtil_InitMaterial()
  33. // Desc: Helper function called to build a D3DMATERIAL7 structure
  34. //-----------------------------------------------------------------------------
  35. VOID D3DUtil_InitMaterial( D3DMATERIAL7& mtrl, FLOAT r, FLOAT g, FLOAT b,
  36.                            FLOAT a )
  37. {
  38.     ZeroMemory( &mtrl, sizeof(D3DMATERIAL7) );
  39.     mtrl.dcvDiffuse.r = mtrl.dcvAmbient.r = r;
  40.     mtrl.dcvDiffuse.g = mtrl.dcvAmbient.g = g;
  41.     mtrl.dcvDiffuse.b = mtrl.dcvAmbient.b = b;
  42.     mtrl.dcvDiffuse.a = mtrl.dcvAmbient.a = a;
  43. }
  44.  
  45. //-----------------------------------------------------------------------------
  46. // Name: D3DUtil_InitLight()
  47. // Desc: Initializes a D3DLIGHT7 structure
  48. //-----------------------------------------------------------------------------
  49. VOID D3DUtil_InitLight( D3DLIGHT7& light, D3DLIGHTTYPE ltType,
  50.                         FLOAT x, FLOAT y, FLOAT z )
  51. {
  52.     ZeroMemory( &light, sizeof(D3DLIGHT7) );
  53.     light.dltType        = ltType;
  54.     light.dcvDiffuse.r   = 1.0f;
  55.     light.dcvDiffuse.g   = 1.0f;
  56.     light.dcvDiffuse.b   = 1.0f;
  57.     light.dcvSpecular    = light.dcvDiffuse;
  58.     light.dvPosition.x   = light.dvDirection.x = x;
  59.     light.dvPosition.y   = light.dvDirection.y = y;
  60.     light.dvPosition.z   = light.dvDirection.z = z;
  61.     light.dvAttenuation0 = 1.0f;
  62.     light.dvRange        = D3DLIGHT_RANGE_MAX;
  63. }
  64.  
  65. //-----------------------------------------------------------------------------
  66. // Name: D3DUtil_SetViewMatrix()
  67. // Desc: Given an eye point, a lookat point, and an up vector, this
  68. //       function builds a 4x4 view matrix.
  69. //-----------------------------------------------------------------------------
  70. HRESULT D3DUtil_SetViewMatrix( D3DMATRIX& mat, D3DVECTOR& vFrom,
  71.                                D3DVECTOR& vAt, D3DVECTOR& vWorldUp )
  72. {
  73.     // Get the z basis vector, which points straight ahead. This is the
  74.     // difference from the eyepoint to the lookat point.
  75.     D3DVECTOR vView = vAt - vFrom;
  76.  
  77.     FLOAT fLength = Magnitude( vView );
  78.     if( fLength < 1e-6f )
  79.         return E_INVALIDARG;
  80.  
  81.     // Normalize the z basis vector
  82.     vView /= fLength;
  83.  
  84.     // Get the dot product, and calculate the projection of the z basis
  85.     // vector onto the up vector. The projection is the y basis vector.
  86.     FLOAT fDotProduct = DotProduct( vWorldUp, vView );
  87.  
  88.     D3DVECTOR vUp = vWorldUp - fDotProduct * vView;
  89.  
  90.     // If this vector has near-zero length because the input specified a
  91.     // bogus up vector, let's try a default up vector
  92.     if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
  93.     {
  94.         vUp = D3DVECTOR( 0.0f, 1.0f, 0.0f ) - vView.y * vView;
  95.  
  96.         // If we still have near-zero length, resort to a different axis.
  97.         if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
  98.         {
  99.             vUp = D3DVECTOR( 0.0f, 0.0f, 1.0f ) - vView.z * vView;
  100.  
  101.             if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
  102.                 return E_INVALIDARG;
  103.         }
  104.     }
  105.  
  106.     // Normalize the y basis vector
  107.     vUp /= fLength;
  108.  
  109.     // The x basis vector is found simply with the cross product of the y
  110.     // and z basis vectors
  111.     D3DVECTOR vRight = CrossProduct( vUp, vView );
  112.  
  113.     // Start building the matrix. The first three rows contains the basis
  114.     // vectors used to rotate the view to point at the lookat point
  115.     D3DUtil_SetIdentityMatrix( mat );
  116.     mat._m._11 = vRight.x;    mat._m._12 = vUp.x;    mat._m._13 = vView.x;
  117.     mat._m._21 = vRight.y;    mat._m._22 = vUp.y;    mat._m._23 = vView.y;
  118.     mat._m._31 = vRight.z;    mat._m._32 = vUp.z;    mat._m._33 = vView.z;
  119.  
  120.     // Do the translation values (rotations are still about the eyepoint)
  121.     mat._m._41 = - DotProduct( vFrom, vRight );
  122.     mat._m._42 = - DotProduct( vFrom, vUp );
  123.     mat._m._43 = - DotProduct( vFrom, vView );
  124.  
  125.     return S_OK;
  126. }
  127.  
  128. //-----------------------------------------------------------------------------
  129. // Name: D3DUtil_SetProjectionMatrix()
  130. // Desc: Sets the passed in 4x4 matrix to a perpsective projection matrix built
  131. //       from the field-of-view (fov, in y), aspect ratio, near plane (D),
  132. //       and far plane (F). Note that the projection matrix is normalized for
  133. //       element [3][4] to be 1.0. This is performed so that W-based range fog
  134. //       will work correctly.
  135. //-----------------------------------------------------------------------------
  136. HRESULT D3DUtil_SetProjectionMatrix( D3DMATRIX& mat, FLOAT fFOV, FLOAT fAspect,
  137.                                      FLOAT fNearPlane, FLOAT fFarPlane )
  138. {
  139.     if( fabs(fFarPlane-fNearPlane) < 0.01f )
  140.         return E_INVALIDARG;
  141.     if( fabs(sin(fFOV/2)) < 0.01f )
  142.         return E_INVALIDARG;
  143.  
  144.     FLOAT w = fAspect * ( cosf(fFOV/2)/sinf(fFOV/2) );
  145.     FLOAT h =   1.0f  * ( cosf(fFOV/2)/sinf(fFOV/2) );
  146.     FLOAT Q = fFarPlane / ( fFarPlane - fNearPlane );
  147.  
  148.     ZeroMemory( &mat, sizeof(D3DMATRIX) );
  149.     mat._m._11 = w;
  150.     mat._m._22 = h;
  151.     mat._m._33 = Q;
  152.     mat._m._34 = 1.0f;
  153.     mat._m._43 = -Q*fNearPlane;
  154.  
  155.     return S_OK;
  156. }
  157.  
  158. //-----------------------------------------------------------------------------
  159. // Name: D3DUtil_SetRotateXMatrix()
  160. // Desc: Create Rotation matrix about X axis
  161. //-----------------------------------------------------------------------------
  162. VOID D3DUtil_SetRotateXMatrix( D3DMATRIX& mat, FLOAT fRads )
  163. {
  164.     D3DUtil_SetIdentityMatrix( mat );
  165.     mat._m._22 =  cosf( fRads );
  166.     mat._m._23 =  sinf( fRads );
  167.     mat._m._32 = -sinf( fRads );
  168.     mat._m._33 =  cosf( fRads );
  169. }
  170.  
  171. //-----------------------------------------------------------------------------
  172. // Name: D3DUtil_SetRotateYMatrix()
  173. // Desc: Create Rotation matrix about Y axis
  174. //-----------------------------------------------------------------------------
  175. VOID D3DUtil_SetRotateYMatrix( D3DMATRIX& mat, FLOAT fRads )
  176. {
  177.     D3DUtil_SetIdentityMatrix( mat );
  178.     mat._m._11 =  cosf( fRads );
  179.     mat._m._13 = -sinf( fRads );
  180.     mat._m._31 =  sinf( fRads );
  181.     mat._m._33 =  cosf( fRads );
  182. }
  183.  
  184. //-----------------------------------------------------------------------------
  185. // Name: D3DUtil_SetRotateZMatrix()
  186. // Desc: Create Rotation matrix about Z axis
  187. //-----------------------------------------------------------------------------
  188. VOID D3DUtil_SetRotateZMatrix( D3DMATRIX& mat, FLOAT fRads )
  189. {
  190.     D3DUtil_SetIdentityMatrix( mat );
  191.     mat._m._11  =  cosf( fRads );
  192.     mat._m._12  =  sinf( fRads );
  193.     mat._m._21  = -sinf( fRads );
  194.     mat._m._22  =  cosf( fRads );
  195. }
  196.  
  197. //-----------------------------------------------------------------------------
  198. // Name: D3DUtil_SetRotationMatrix
  199. // Desc: Create a Rotation matrix about vector direction
  200. //-----------------------------------------------------------------------------
  201. VOID D3DUtil_SetRotationMatrix( D3DMATRIX& mat, D3DVECTOR& vDir, FLOAT fRads )
  202. {
  203.     FLOAT     fCos = cosf( fRads );
  204.     FLOAT     fSin = sinf( fRads );
  205.     D3DVECTOR v    = Normalize( vDir );
  206.  
  207.     mat._m._11 = ( v.x * v.x ) * ( 1.0f - fCos ) + fCos;
  208.     mat._m._12 = ( v.x * v.y ) * ( 1.0f - fCos ) - (v.z * fSin);
  209.     mat._m._13 = ( v.x * v.z ) * ( 1.0f - fCos ) + (v.y * fSin);
  210.  
  211.     mat._m._21 = ( v.y * v.x ) * ( 1.0f - fCos ) + (v.z * fSin);
  212.     mat._m._22 = ( v.y * v.y ) * ( 1.0f - fCos ) + fCos ;
  213.     mat._m._23 = ( v.y * v.z ) * ( 1.0f - fCos ) - (v.x * fSin);
  214.  
  215.     mat._m._31 = ( v.z * v.x ) * ( 1.0f - fCos ) - (v.y * fSin);
  216.     mat._m._32 = ( v.z * v.y ) * ( 1.0f - fCos ) + (v.x * fSin);
  217.     mat._m._33 = ( v.z * v.z ) * ( 1.0f - fCos ) + fCos;
  218.  
  219.     mat._m._14 = mat._m._24 = mat._m._34 = 0.0f;
  220.     mat._m._41 = mat._m._42 = mat._m._43 = 0.0f;
  221.     mat._m._44 = 1.0f;
  222. }
  223.